package com.kk.xhr.admin.biz.impl;

import com.kk.xhr.admin.biz.IRewardBizService;
import com.kk.xhr.admin.controller.request.RewardAddRequest;
import com.kk.xhr.admin.controller.request.RewardModifyRequest;
import com.kk.xhr.admin.controller.request.RewardQueryRequest;
import com.kk.xhr.admin.controller.vo.RewardVO;
import com.kk.xhr.common.pojo.BinaryEnum;
import com.kk.xhr.model.dto.PageDto;
import com.kk.xhr.model.dto.RewardDTO;
import com.kk.xhr.model.dto.RewardQueryRequestDTO;
import com.kk.xhr.model.service.IRewardService;
import com.kk.xhr.model.service.IUserService;
import com.kk.xhr.util.DateUtil;
import com.kk.xhr.web.vo.PageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 奖惩biz服务接口实现
 *
 * @author kkmystery
 * @version 1.0 2021/3/7
 * @since 1.0.0
 */
@Repository
@RequiredArgsConstructor
public class RewardBizServiceImpl implements IRewardBizService {
    private final IRewardService rewardService;
    private final IUserService userService;

    @Override
    public PageVO<RewardVO> queryPage(RewardQueryRequest request) {
        PageDto<RewardDTO> dtos = rewardService.queryPage(RewardQueryRequestDTO.builder()
            .userId(request.getUserId())
            .type(request.getType())
            .status(request.getStatus())
            .limit(request.getLimit())
            .offset(request.getOffset())
            .build());
        if (0 == dtos.getTotal()) {
            return PageVO.empty();
        }
        return PageVO.fromDTO(dtos, this::generateVO);
    }

    @Override
    public List<RewardVO> queryMine(Long userId) {
        return rewardService.queryByUserId(userId)
            .stream()
            .map(this::generateVO)
            .collect(Collectors.toList());
    }

    @Override
    public boolean add(RewardAddRequest request) {
        return rewardService.add(request.getUserIds(), RewardDTO.builder()
            .money(request.getMoney())
            .reason(request.getReason())
            .rewardDate(DateUtil.validTime(request.getRewardDate()))
            .type(request.getType())
            .build());
    }

    @Override
    public boolean modify(Long id, RewardModifyRequest request) {
        return rewardService.modify(RewardDTO.builder()
            .id(id)
            .money(request.getMoney())
            .reason(request.getReason())
            .rewardDate(DateUtil.validTime(request.getRewardDate()))
            .type(request.getType())
            .build());
    }

    @Override
    public boolean modifyStatus(Long id, BinaryEnum status) {
        return rewardService.modifyStatus(id, status);
    }

    /**
     * RewardVO生成器
     *
     * @param dto RewardDTO
     * @return RewardVO
     */
    private RewardVO generateVO(RewardDTO dto) {
        return RewardVO.builder()
            .id(dto.getId())
            .name(userService.queryName(dto.getUserId()))
            .money(dto.getMoney())
            .reason(dto.getReason())
            .rewardDate(dto.getRewardDate())
            .type(dto.getType())
            .status(dto.getStatus())
            .build();
    }
}
